package com.ct.gd.passport.domain;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;

import android.os.Parcel;
import android.os.Parcelable;

import com.ct.gd.passport.common.constants.Config.Area;
import com.fuckhtc.gson.annotations.SerializedName;
import com.up91.common.android.connect.Params;

/**
 * 出行申请表单数据类
 * 
 * @author Administrator
 * 
 */
public class PassportGoApply implements Parcelable {

	public static final String KEY_AREA_VALUE = "clbh";
	public static final String KEY_PASSPORT_ID = "zjhm";
	public static final String KEY_PASS_LIMIT = "zjyxqz";
	public static final String KEY_BIRTHDAY = "csrq";
	public static final String KEY_NAME = "xm";
	public static final String KEY_FNAME = "xmx";
	public static final String KEY_SNAME = "xmm";
	public static final String KEY_UNDER_WRITE = "underwrite";
	public static final String KEY_PHONE_NUMBER = "dwdh";
	public static final String KEY_EMS = "sd";
	public static final String KEY_EMS_NAME = "emssjr";
	public static final String KEY_EMS_ADDR = "emsaddr";
	public static final String KEY_POSTCORD = "emsyb";
	public static final String KEY_GO_AREA = "qwd";
	public static final String KEY_GO_TYPE = "type";
	public static final String KEY_VALIDATE_CODE = "validatecode";
	public static final String KEY_SERIAL_ID = "serial";

	@SerializedName("serialNumber")
	/** 通行证号码 */
	String passportId;

	@SerializedName("birthday")
	String birthday;

	@SerializedName("validate")
	/** 通行证有效期 */
	String passLimitDate;

	@SerializedName("district")
	/** 常住户口所在地 */
	String areaValue;

	@SerializedName("name")
	/** 姓名 */
	String name;

	@SerializedName("namex")
	/** 姓 */
	String firstName;

	@SerializedName("namem")
	/** 名 */
	String secondName;

	@SerializedName("phone")
	/** 申请人联系电话 */
	String phoneNumber;

	@SerializedName("expressName")
	/** 收件人姓名 */
	String emsName;

	@SerializedName("expressAdress")
	/** 收件地址 */
	String emsAddr;

	@SerializedName("expressCode")
	/** 邮政编码 */
	String postCord;

	Area area;

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
		if (area != null) {
			this.goAreaCode = area.code;
		}
	}

	@SerializedName("goArea")
	/** 签注地区 */
	String goAreaCode;

	public String getGoAreaCode() {
		return goAreaCode;
	}

	public void setGoAreaCode(String goAreaCode) {
		this.setArea(Area.getAreaByCode(goAreaCode));
	}

	@SerializedName("type")
	/** 申请签注种类 */
	String goType;

	@SerializedName("underwrite")
	String underWrite;

	@SerializedName("express")
	/** 需要快递 */
	boolean ems;

	String validateCode;

	@SerializedName("serial")
	/** 受理编号 */
	String serial;

	HashMap<String, String> params = new HashMap<String, String>();

	public String getPassportId() {
		return passportId;
	}

	public void setPassportId(String passportId) {
		this.passportId = passportId;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getPassLimitDate() {
		return passLimitDate;
	}

	public void setPassLimitDate(String passLimitDate) {
		this.passLimitDate = passLimitDate;
	}

	public String getAreaValue() {
		return areaValue;
	}

	public void setAreaValue(String areaValue) {
		this.areaValue = areaValue;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmsName() {
		return emsName;
	}

	public void setEmsName(String emsName) {
		this.emsName = emsName;
	}

	public String getEmsAddr() {
		return emsAddr;
	}

	public void setEmsAddr(String emsAddr) {
		this.emsAddr = emsAddr;
	}

	public String getPostCord() {
		return postCord;
	}

	public void setPostCord(String postCord) {
		this.postCord = postCord;
	}

	public String getGoType() {
		return goType;
	}

	public void setGoType(String goType) {
		this.goType = goType;
	}

	public String getUnderWrite() {
		return underWrite;
	}

	public void setUnderWrite(String underWrite) {
		this.underWrite = underWrite;
	}

	public boolean getEms() {
		return ems;
	}

	public void setEms(boolean ems) {
		this.ems = ems;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getSecondName() {
		return secondName;
	}

	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}

	public String getGoAreaLongCode() {
		return area.code + "ZJYXQ";
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public void putParam(String key, String value) {
		params.put(key, value);
	}

	public void collectParams() {
		params.put("clbh", getAreaValue());
		params.put("zjhm", getPassportId());
		params.put("zjyxqz", getPassLimitDate());
		params.put("csrq", getBirthday());
		try {
			params.put("xm", URLEncoder.encode(getName(), "utf-8"));
			params.put("xmx", URLEncoder.encode(getFirstName(), "utf-8"));
			params.put("xmm", URLEncoder.encode(getSecondName(), "utf-8"));
			params.put("underwrite", URLEncoder.encode(getName(), "utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		params.put("dwdh", getPhoneNumber());
		params.put("sd", Boolean.toString(getEms()));
		params.put("emssjr", getEmsName());
		params.put("emsaddr", getEmsAddr());
		params.put("emsyb", getPostCord());
		params.put("qwd", getArea().code);
		params.put(getGoAreaLongCode(), getGoType());
		params.put("validatecode", getValidateCode());
		if (serial != null) {
			params.put("serial", serial);
		}
	}

	public Params getParams(String[] keys) {
		Params ps = new Params();
		for (int i = keys.length; --i >= 0;) {
			String value = params.get(keys[i]);
			if (value != null) {
				ps.put(keys[i], value);
			}
		}
		return ps;
	}

	public Params getParamsChangeKey(String[] keys, String[] toKeys) {
		if (keys.length != toKeys.length) {
			toKeys = keys;
		}
		Params ps = new Params();
		for (int i = keys.length; --i >= 0;) {
			String value = params.get(keys[i]);
			if (keys[i].equals("xmx") || keys[i].equals("xmm")) {
				value = URLDecoder.decode(value);
			}
			if (value != null) {
				ps.put(toKeys[i], value);
			}
		}
		return ps;
	}

	@Override
	public boolean equals(Object o) {
		return (o instanceof PassportGoApply) && this.serial != null
				&& this.serial.equals(((PassportGoApply) o).serial);
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(passportId);
		dest.writeString(passLimitDate);
		dest.writeString(birthday);
		dest.writeString(areaValue);
		dest.writeString(name);
		dest.writeString(firstName);
		dest.writeString(secondName);
		dest.writeString(phoneNumber);
		dest.writeInt(ems ? 1 : 0);
		dest.writeString(emsName);
		dest.writeString(emsAddr);
		dest.writeString(postCord);
		dest.writeString(goAreaCode);
		dest.writeString(goType);
		dest.writeString(serial);
	}

	public static final Parcelable.Creator<PassportGoApply> CREATOR = new Creator<PassportGoApply>() {

		@Override
		public PassportGoApply[] newArray(int size) {
			return new PassportGoApply[size];
		}

		@Override
		public PassportGoApply createFromParcel(Parcel src) {
			PassportGoApply apply = new PassportGoApply();
			apply.passportId = src.readString();
			apply.passLimitDate = src.readString();
			apply.setBirthday(src.readString());
			apply.areaValue = src.readString();
			apply.name = src.readString();
			apply.firstName = src.readString();
			apply.secondName = src.readString();
			apply.phoneNumber = src.readString();
			apply.ems = src.readInt() == 1;
			apply.emsName = src.readString();
			apply.emsAddr = src.readString();
			apply.postCord = src.readString();
			apply.setGoAreaCode(src.readString());
			apply.goType = src.readString();
			apply.serial = src.readString();
			return apply;
		}
	};

}
